package
{
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class Entity
	{
		public static const TEAM_RED:int = 1;
		public static const TEAM_BLUE:int = 2;
		public var id:String;
		public var speed:Number = 50.0;
		public var display:Sprite;
		
		public var finalLocation:Point;
		public var currentLocation:Point;
		public var nextLocation:Point;
		public var isMovingTowardsNewDestination:Boolean;
		public var distanceToTravel:Number;
		
		
		public var isMob:Boolean = false;
		public var isPlayer:Boolean = false;
		public function Entity()
		{
			isMovingTowardsNewDestination = false;
			finalLocation = currentLocation = new Point(0,0);
		}
		
		
		public function initAsMob():void{
			isMob = true;
			display = new Sprite();
			display.graphics.beginFill(0);
			display.graphics.drawCircle(0,0,5);
			display.graphics.endFill();			
		}		
		
		public var playerTeam:int;
		public function initAsPlayer(team:int, side:Sprite):void{
			this.playerTeam = team;
			var color:uint;
			switch(playerTeam){
				case 1:
					color = 0xff0000;
					break;
				case 2:
					color = 0x0000ff;
					break;
			}
			isPlayer = true;
			display = new Sprite();
			display.graphics.lineStyle(2,0);
			display.graphics.beginFill(color);
			display.graphics.drawCircle(0,0,10);
			display.graphics.endFill();	

			this.display.y = side.y + side.height/2;
			this.display.x = side.x + side.width/2;
			
			this.currentLocation = this.nextLocation = new Point(this.display.x, this.display.y);
			
		}
		
		public function moveTo(newLoc:Point):void{
			finalLocation = newLoc;
			isMovingTowardsNewDestination = true;
			distanceToTravel = newLoc.subtract(currentLocation).length;
		}
		
		public function updateDisplay():void{
			display.x = currentLocation.x;
			display.y = currentLocation.y;
		}
		
		public function update(delta:int):void{
			
			if(nextLocation){
				currentLocation = nextLocation;
				updateDisplay();
				nextLocation = null;
			}
			
			if(isMovingTowardsNewDestination){
				var distance:Number = delta*speed*.001; 
				distanceToTravel -= distance;
				if(distanceToTravel <= 0){
					isMovingTowardsNewDestination = false;
					currentLocation = finalLocation;
				}
				var direction:Point = finalLocation.subtract(currentLocation);
				direction.normalize(distance);
				nextLocation = currentLocation.add(direction);
			}
		}
		
		public function serialize():Object{
			var ser:Object = new Object();
			ser['id'] = id;
			ser['speed'] = speed;
			ser['playerTeam'] = playerTeam;
			ser['curLocation'] = Util.serializePoint(currentLocation);
			ser['finalLocation'] = Util.serializePoint(finalLocation);
			ser['nextLocation'] = Util.serializePoint(nextLocation);
			ser['isMovingTowardsNewDestination'] = isMovingTowardsNewDestination;
			ser['distanceToTravel'] = distanceToTravel;
			ser['isPlayer'] = isPlayer;
			ser['isMob'] = isMob;
			
			return ser;
		}
		
		public function unserialize(obj:Object):void{
			id = obj['id'];
			speed = obj['speed'];
			playerTeam = obj['playerTeam'];
			currentLocation = Util.unserializePoint(obj['curLocation']);
			finalLocation = Util.unserializePoint(obj['finalLocation']);
			nextLocation = Util.unserializePoint(obj['nextLocation']);
			isMovingTowardsNewDestination = obj['isMovingTowardsNewDestination'];
			distanceToTravel = obj['distanceToTravel'];
			isPlayer = obj['isPlayer'];
			isMob = obj['isMob'];
		}
		
		public static function serializeVector(vector:Vector.<Entity>):Array{
			var i:int, il:int;
			var serArray:Array = new Array();
			for(i = 0, il = vector.length; i < il; i++){
				serArray.push(vector[i].serialize());
			}
			
			return serArray;
		}
		
		public static function unserializeVector(ser:Array):Vector.<Entity>{
			var entities:Vector.<Entity> = new Vector.<Entity>;
			for each(var obj:Object in ser){
				var entity:Entity = new Entity();
				entity.unserialize(obj);
				entities.push(entity);
			}
			
			return entities;
		}
	}
}